package com.laze.mywanandroid.app

import android.app.Application
import com.hjq.toast.ToastUtils
import com.hjq.toast.style.ToastAliPayStyle
import com.hjq.toast.style.ToastQQStyle
import com.hjq.toast.style.ToastWhiteStyle
import com.laze.mywanandroid.commom.ActivityLifecycleCallbacksAdapter
import com.laze.mywanandroid.utils.core.ActivityUtil
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.FormatStrategy
import com.orhanobut.logger.Logger
import com.orhanobut.logger.PrettyFormatStrategy

/**
 * 参考：
 * 1.https://www.jianshu.com/p/214d4dea6029  github:https://github.com/xuanduoduo/MVVM
 * 2.https://github.com/xiaoyanger0825/wanandroid
 */

class App : Application() {

    companion object {
        lateinit var instance: App
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        rigesterActivityCallbacks()

        //toast初始化，QQ风格
        ToastUtils.init(this,ToastQQStyle(this));

        //初始化logger
        val formatStrategy: FormatStrategy = PrettyFormatStrategy.newBuilder()
            .tag("laze_info") // (Optional) Global tag for every log. Default PRETTY_LOGGER
            .build()
        Logger.addLogAdapter(AndroidLogAdapter(formatStrategy))
    }

    private fun rigesterActivityCallbacks() {
        registerActivityLifecycleCallbacks(ActivityLifecycleCallbacksAdapter(
            onActivityCreated = { activity, _ ->
                ActivityUtil.activities.add(activity)
            },
            onActivityDestroyed = { activity ->
                ActivityUtil.activities.remove(activity)
            }
        ))
    }
}